<template>
	<view class="box">
		<u-navbar :is-back="true" title="注册"></u-navbar>
		<view class="container">
			<view class="logo">
				<u-image width="166rpx" height="116rpx" :src="logo"></u-image>
			</view>
			<view class="title cxc">
				账户注册
			</view>
			<u-form :model="form" ref="form1" :error-type="errorType">
				<u-form-item prop="phone">
					<view class="ipt flc">
						<view class="label" @click="()=>this.select1.show = true">
							<span>{{label}}</span> <u-icon name="arrow-down" size="20" color="#949599"></u-icon>
						</view>
						<u-input class="ml2" v-model="form.phone" placeholder="请输入注册手机号" />
					</view>
				</u-form-item>
				<u-form-item prop="code">
					<view class="ipt flc">
						<view class="icon">
							<u-icon :name="'/static/icons/aq.png'" size="26"></u-icon>
						</view>
						<view class="code ml2" style="width: 500rpx;">
							<u-input :border="false" placeholder="请输入验证码" v-model="form.code" type="text"></u-input>
						</view>

						<u-button class="" slot="right" :custom-style="customStyle" size="mini"
							@click="getCodes">{{codeTips}}</u-button>
					</view>
				</u-form-item>
				<u-form-item prop="password">
					<view class="ipt flc">
						<view class="icon">
							<u-icon :name="'/static/icons/lock.png'" size="26"></u-icon>
						</view>
						<u-input class="ml2 password" type="password" v-model="form.password" placeholder="请输入密码" />
					</view>
				</u-form-item>
				<u-form-item prop="rPassword">
					<view class="ipt flc">
						<view class="icon">
							<u-icon :name="'/static/icons/lock.png'" size="26"></u-icon>
						</view>
						<u-input class="ml2 password" type="password" v-model="form.rPassword" placeholder="请再次输入密码" />
					</view>
				</u-form-item>
			</u-form>
			<view class="aggrement flc ">
				<view class="agree" @click="radioChange">
					<u-icon v-if="check" :name="'/static/icons/select.png'" size="32"></u-icon>
					<u-icon v-if="!check" :name="'/static/icons/unselect.png'" size="32"></u-icon>
				</view>
				<view class="aggrementC ml2">
					我已阅读并同意<span class="cxc" @click="goPancy(2)">《用户协议》</span> 和<span class="cxc"
						@click="goPancy(3)">《隐私政策》</span>
				</view>
			</view>
			<view class="btn" @click="submit">
				立即注册
			</view>
			<u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
			<view class="tips " @click="login">
				<view class="tip flc">
					<span class="r ptc">已有账号，</span>
					<span class="l cxc">立即登录</span>
				</view>
			</view>
		</view>
		<u-select v-model="select1.show" :list="select1.list" @confirm="onSelect1"></u-select>


	</view>

</template>

<script>
	import {
		getLogo
	} from '../../api/index';
	import {
		register,
		getCode
	} from '../../api/login';
	import {
		toastC
	} from '../../utils/common';
	export default {
		data() {
			return {
				errorType: ['toast'],
				form: {
					phone: '',
					password: "",
					rPassword: "",
					code: "",
				},
				rules: {
					phone: [{
						required: true,
						message: '请输入手机号',
						trigger: ['change', 'blur']
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: ['change', 'blur']
					}],
					rPassword: [{
							required: true,
							message: '请再次输入密码',
							trigger: ['change', 'blur']
						},
						{
							validator: (rule, value, callback) => {
								return value == this.form.password;
							},
							message: '两次密码不一致'
						},
					],
					code: [{
						required: true,
						message: '请输入验证码',
						trigger: ['change', 'blur']
					}],

				},
				logo: "",
				select1: {
					show: false,
					current: {},
					list: [{
							value: 1,
							label: '+86'
						},
						{
							value: 2,
							label: '+44'
						},
						{
							value: 3,
							label: '+1'
						},
						{
							value: 4,
							label: '+91'
						}
					],
				},
				label: "+86",
				codeTips: '',
				check: false,
				customStyle: {
					background: '#fc4825',
					color: "#fff",
					border: "none"
				}
			};
		},
		onReady() {
			this.$refs.form1.setRules(this.rules);
		},
		onLoad() {
			getLogo().then(res => {
				this.logo = res.data.logo
			})
		},
		methods: {
			onSelect1(arr) {
				let current = arr[0];
				let value = current.label;
				this.select1.current = current;
				this.label = value

			},
			submit() {
				this.$refs.form1.validate((valid) => {
					console.log(valid);
					if (valid) {
						register({
							phone:this.form.phone,
							sms_code:this.form.code,
							password:this.form.password,
							password_confirmation:this.form.rPassword
						}).then(res => {
							console.log(res);
							if (res.code == 200) {
								toastC("注册成功")
								setTimeout(() => {
									uni.navigateBack()
								}, 300)
							}
						})
					}
				})
			},
			onCalendar1(e) {
				this.form.birthday = e.result;
			},
			login() {
				uni.navigateTo({
					url: '/page/login/login'
				})
			},
			codeChange(text) {
				this.codeTips = text;
			},
			radioChange() {
				this.check = !this.check
			},
			goPancy(index) {
				uni.navigateTo({
					url: '/other/webData/webData?type=' + index
				})
			},
			getCodes() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})
					getCode().then(res=>{
						if(res.code==200){
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							this.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						}
					})
					
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		margin-top: 20rpx;
		background-color: #fff;
		width: 750rpx;
		height: calc(100vh - 96rpx);
		box-sizing: border-box;
		padding: 20rpx;
	}

	.logo {
		width: 200rpx;
		height: 200rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 10rpx 20rpx 0rpx rgba(51, 51, 51, 0.1);
		border-radius: 30rpx;
		margin: 40rpx auto;
		box-sizing: border-box;
		padding: 42rpx 17rpx;
	}

	.title {
		height: 40rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		font-size: 42rpx;
		margin: 40rpx 0;
	}

	.label {
		min-width: 60rpx;
	}

	.forgetPassword {
		text-align: right;
		font-size: 28rpx;
		margin: 20rpx 0;
	}

	.btn {
		width: 650rpx;
		height: 80rpx;
		background: linear-gradient(90deg, #FD4120, #F47749);
		border-radius: 40rpx;
		margin: 40rpx auto;
		text-align: center;
		line-height: 80rpx;
		font-size: 30rpx;
		color: #FFFFFF;

	}

	.tips {
		font-size: 28rpx;
		text-align: center;
		width: 710rpx;
		display: flex;
		justify-content: center;
	}

	.ml6 {
		margin-left: 60rpx;
	}

	.aggrement {
		margin-top: 40rpx;
		width: 710rpx;
		box-sizing: border-box;

		.agree {
			width: 32rpx;
			height: 32rpx;
			border-radius: 50%;
		}

		.aggrementC {
			height: 32rpx;
			line-height: 32rpx;
		}
	}
</style>